﻿@using ST = EmoneyWeiXinCommon.StringTools;
@{
    ViewBag.fieldvalue = "消息模板管理";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="right">
    <h3>消息模板管理</h3>
    <!--manage-->
    <div class="manage">
        <!--manage_top-->
        <div class="manage_top">
            @*<ul class="manage_top_ul">
            </ul>*@
            <div class="manage_register">
                <p><span>模板ID：</span><input id="modelid" type="text" readonly="true" value="@Html.Raw(ViewBag.NewsModel.modelid)"><input style="display: none" id="hidid" type="text"></p>
                <p><span>模板标题：</span><input id="title" type="text" readonly="true" value="@Html.Raw(ViewBag.NewsModel.title)"></p>
            </div>
            <div class="manage_register manage_register02">
                <p><span>模板内容：</span><textarea id="content" readonly="readonly">@Html.Raw(ViewBag.NewsModel.content)</textarea></p>
            </div>
            <div class="manage_register">
                <p><span>模板说明：</span><input id="modelmemo" type="text" readonly="true" value="@Html.Raw(ViewBag.NewsModel.remark)"></p>
            </div>
            <div class="manage_register">
                <p><span>定义字段：</span><input id="fieldname" type="text"></p>
                <p><span>字段内容：</span><input id="fieldvalue" type="text"></p>
            </div>
            <div class="manage_register">
                <p><span>字段样式：</span><input id="style" type="text"></p>
                <p><span>说明：</span><input id="memo" type="text"></p>
            </div>
            <p class="manage_add"><a id="btnSave" style='display:@(new EmoneyWeiXin.Controllers.BaseConfigurationController().DisplayOperate(2, 3,ViewBag.leftid))'>保存</a><a id="btnCancel" class="manage_add_font">取消</a><a id="btnBack" class="manage_add_font">返回</a></p>
        </div>
        <!--manage_top end-->
        <p class="manage_line"></p>
        <!--manage_bottom-->
        <div class="manage_bottom">
            <table border="0" cellpadding="0" cellspacing="0" width="100%">
                <tr>
                    <th><span>编号</span></th>
                    <th><span>定义字段</span></th>
                    <th><span>字段内容</span></th>
                    <th><span>字段样式</span></th>
                    <th><span>字段说明</span></th>
                    <th><span>操作</span></th>
                </tr>
                <tbody id="dvList">
                </tbody>
            </table>
            <div class="manage_page" id="manage_page">
            </div>
        </div>
        <!--manage_bottom end-->
    </div>
    <!--manage end-->
</div>

<script type="text/javascript">
    var weixinid = "";
    var pageIndex = 1;
    var pageSize = 10;
    var pageCount = 1;
    var totalCount = 0;
    $(document).ready(function () {
        showNewsModel();
        gotoPage();
        $("#btnSave").click(function () {
            if (ischeck()) {
                var id = $("#hidid").val();
                var fieldname = $("#fieldname").val();
                var fieldvalue = $("#fieldvalue").val();
                var style = $("#style").val();
                var memo = $("#memo").val();
                var mid = "@ViewBag.NewsModel.id";
                if (id > 0) {
                    operate("2", id, mid, fieldname, fieldvalue, style, memo);
                }
                else {
                    operate("1", "0", mid, fieldname, fieldvalue, style, memo);
                }
            }
        });
        $("#btnCancel").click(function () {
            reseting();
        });
        $("#btnBack").click(function () {
            window.location.href = "@ST.GetTrendsFile("NewsModel/NewsModelList?leftid="+ViewBag.leftid)";
         });
    });

    function showNewsModel() {
        //$("#modelid").val("@Html.Raw(ViewBag.modelid)");
        //$("#title").val("@Html.Raw(ViewBag.title)");
        //$("#content").val("@Html.Raw(ViewBag.content)");
        //$("#modelmemo").val("@Html.Raw(ViewBag.memo)");
        weixinid = "@Html.Raw(ViewBag.weixinid)";
    }

    function ischeck() {
        var fieldname = $("#fieldname").val();
        if (fieldname == "") {
            alert("定义字段不能空，请重新输入！");
            $("#fieldname").focus();
            return false;
        }
        var fieldvalue = $("#fieldvalue").val();
        if (fieldvalue == "") {
            alert("字段内容不能空，请重新输入！");
            $("#fieldvalue").focus();
            return false;
        }
        var style = $("#style").val();
        if (style == "") {
            alert("字段样式不能空，请重新输入！");
            $("#style").focus();
            return false;
        }
        var memo = $("#memo").val();
        if (memo == "") {
            alert("说明不能空，请重新输入！");
            $("#memo").focus();
            return false;
        }
        return true;
    }

    var ajaxdata = {};
    function gotoPage() {
        ajaxdata["mid"] = "@ViewBag.NewsModel.id";
        ajaxdata["pindex"] = pageIndex;
        ajaxdata["psize"] = pageSize;
        $.ajax({
            url: "@ST.GetTrendsFile("NewsModel/QueryNewsModelField")",
            type: "POST",
            async: true,
            data: ajaxdata,
            beforeSend: function () {
                //showLoading();
            },
            complete: function () {
                //iddenLoading();
            },
            success: function (data) {
                if (data != "" && data != null && data != undefined) {
                    var arrData = data.split("^");
                    totalCount = arrData[0];
                    data = arrData[1];
                    var d = eval(data);
                    var html = "";
                    if (d != "" && d != null && d != undefined) {
                        for (var i = 0; i < d.length; i++) {
                            html += "<tr>";
                            html += getTxtTd(d[i]["id"]) + getTxtTd(d[i]["fieldname"]) + getTxtTd(d[i]["fieldvalue"]) + getTxtTd(d[i]["style"]) + getTxtTd(d[i]["remark"]);
                            html += "<td><a class=\"manage_bottom_font blue\" onclick=\"selected('" + d[i]["id"] + "','" + d[i]["fieldname"] + "','" + d[i]["fieldvalue"] + "','" + d[i]["style"] + "','" + d[i]["remark"] + "')\" style='display:@(new EmoneyWeiXin.Controllers.BaseConfigurationController().DisplayOperate(3,ViewBag.leftid))'>编辑</a>";
                            html += "<a class=\" manage_bottom_font red\" onclick=\"operate('-1','" + d[i]["id"] + "')\" style='display:@(new EmoneyWeiXin.Controllers.BaseConfigurationController().DisplayOperate(4,ViewBag.leftid))'>删除</a></td>";
                            html += '</tr>';
                        }
                    }
                    $("#dvList").html(html);
                    PageCountHtml();
                }
                else {
                    $("#dvList").html("没有查询到相关数据！");
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                //alert(errorThrown);
            }
        });
    }
    function getTxtTd(s) {
        return '<td><span>' + s + '</span></td>';
    }

    //编辑，删除，关闭操作
    function operate(op, id, mid, fieldname, fieldvalue, style, memo) {
        $.ajax({
            url: "@ST.GetTrendsFile("NewsModel/UpdateNewsModelField")",
            type: "POST",
            async: true,
            data: { op: op, id: id, mid: mid, fieldname: fieldname, fieldvalue: fieldvalue, style: style, memo: memo },
            beforeSend: function () {
                //showLoading();
            },
            complete: function () {
                // hiddenLoading();
            },
            success: function (data) {
                if (data == "1") {
                    gotoPage();
                    reseting();
                    alert("操作成功!");

                } else alert(data);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                //alert(errorThrown);
            }
        });
    }
    //重置
    function reseting() {
        $("#hidid").val("0");
        $("#fieldname").val("");
        $("#fieldvalue").val("");
        $("#style").val("");
        $("#memo").val("");
    }
    //选择行
    function selected(id, fieldname, fieldvalue, style, memo) {
        $("#hidid").val(id);
        $("#fieldname").val(fieldname);
        $("#fieldvalue").val(fieldvalue);
        $("#style").val(style);
        $("#memo").val(memo);
    }
    //翻页
    function PageCountHtml() {
        pageCount = Math.ceil(totalCount / pageSize);
        if (pageCount == 0) {
            pageCount = 1;
        }
        $("#manage_page").html(PageHtml(pageIndex, pageCount));
        $("#manage_page .manage_page_right").click(function () {
            pageIndex = parseInt($("#txtPageNum").val()) + parseInt($(this).attr("tname"));
            $("#manage_page").html(PageHtml(pageIndex, pageCount));
            gotoPage();
        });
        $("#manage_page .tiaozhuan").click(function () {
            var pageNum = $("#txtPageNum").val();
            try {
                pageIndex = parseInt(pageNum);
            } catch (e) {
                alert("请输入数字");
                return false;
            }

            if (pageNum > 0 && pageNum <= pageCount) {
                $("#manage_page").html(PageHtml(pageIndex, pageCount));
                gotoPage();
            }
            else {
                alert("当前数字超出范围");
                return false;
            }
        });
    }
</script>
